package com.qimei.onerice.service.impl;

import com.qimei.onerice.dao.OrderDao;
import com.qimei.onerice.dao.OrderDetailDao;
import com.qimei.onerice.dao.impl.OrderDaoImpl;
import com.qimei.onerice.dao.impl.OrderDetailDaoImpl;
import com.qimei.onerice.entity.Dish;
import com.qimei.onerice.entity.Order;
import com.qimei.onerice.entity.OrderDetail;
import com.qimei.onerice.service.DishService;
import com.qimei.onerice.service.OrderDetailService;

import java.util.List;

public class OrderDetailServiceImpl implements OrderDetailService {
	OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
	DishService dishService = new DishServiceImpl();

	@Override
	public List<OrderDetail> getOrderDetailListByOrderId(int orderId) {
		List<OrderDetail> orderDetails = orderDetailDao.getOrderDetailsByOrderId(orderId);
		for (OrderDetail orderDetail : orderDetails) {
			Dish dish = dishService.getDishById(orderDetail.getDishId());
			if (dish != null) {
				orderDetail.setCategoryName(dish.getCategoryName());
				orderDetail.setDishName(dish.getName());
			}
		}
		return orderDetails;
	}

	@Override
	public boolean deleteOrderDetail(int orderDetailId) {
		int result = orderDetailDao.deleteOrderDetail(orderDetailId);
		return result > 0;
	}

	@Override
	public boolean updateOrderDetail(OrderDetail orderDetail) {
		int result = orderDetailDao.updateOrderDetail(orderDetail);
		return result > 0;
	}

	@Override
	public boolean addOrderDetail(OrderDetail orderDetail) {
		int result = orderDetailDao.addOrderDetail(orderDetail);
		return result > 0;
	}

	@Override
	public OrderDetail getOrderDetailByDishId(int orderId, int dishId) {
		return orderDetailDao.getOrderDetailByDishId(orderId, dishId);
	}

	@Override
	public boolean updateQuantity(int orderDetailId, int quantity) {
		return orderDetailDao.updateQuantity(orderDetailId, quantity) > 0;
	}

	@Override
	public OrderDetail getOrderDetailById(int orderDetailId) {
		return orderDetailDao.getOrderDetailById(orderDetailId);
	}

}
